import { save } from  "../../../api/order"
import { getCurrent} from "../../../api/company"


Page({
    data: {
        buyerName:'请选择买家',
        customerId:'',
        supplierName:'请选择卖家',
        supplierId:'', //22
        tradeName:'请选择商品名称',
        productId:'',
        warehouseName:'请选择油库',
        warehouseId:'',
        price: 0,
        addPrice:0,
        quantity:0.5,
        accoManager:'请选择客户经理',
        phone:'',
        memo:"",
        goodsAmount:0,
        submitFlage:true,
        delivery:"1",
        minimum:'1',
        agreementPrice:0
    },
    onLoad(options) {
    },
    onShow(){
        this.calculation()
    },
    watch: function (dataName, callback, that) {
        // 拿到全局变量、当然你也可以监听任何其他的对象
        var obj = this.data;
        Object.defineProperty(obj, dataName, {
            // 可配置
            configurable: true,
            // 可枚举
            enumerable: true,
            // 当监听的函数被设置新值的时候执行
            set: function (value) {
                // 当我们监听的值被修改的时候，触发 set 方法
                // 把新值存入一个临时的属性里，为什么存到临时的？看后边。
                this['temp' + dataName] = value;
                // 调用回调函数，传入新值
                callback.call(that, value);
            },
            // 当监听的函数被get的时候调用
            get: function () {
                // 当我们主动去 get 监听的这个值的时候触发当前函数，这里你可以进行一下其他操作。
                return this['temp' + dataName];
            }
        })
    },
    setAccoManager(e){
        this.data.accoManager = e.detail.name
        this.data.phone = e.detail.phone
        this.setData({
            accoManager:this.data.accoManager,
            phone:this.data.phone
        })
    },
    towarehouseList(){
        wx.navigateTo({
            url: '/pages/pagesGoods/goods/warehouseList?supplierId='+this.data.supplierId
        })
    },
    toSupplier(){
        wx.navigateTo({
            url: '/pages/pagesGoods/goods/user-org-info-select-page'
         })
    },
    toBuyerList(){
        wx.navigateTo({
            url: '/pages/pagesGoods/goods/buyerList'
         })
    },
    toAccoManager(){
        wx.navigateTo({
            url: '/pages/customer/customerManager'
         })
    },
    toTradeName(){
        wx.navigateTo({
            url: '/pages/pagesGoods/goods/search-classify-page?supplierId=' + this.data.supplierId
         })
    },
    priceChange(e){
        this.data.price = e.detail
        this.setData({
            price:this.data.price
        })
        this.calculation()
    },
    calculation(){
        this.data.goodsAmount = this.data.quantity * (this.data.agreementPrice == 0 ? Number(this.data.price): Number(this.data.agreementPrice))
        this.data.goodsAmount = Number(this.data.goodsAmount).toFixed(2)
        this.setData({
            goodsAmount:this.data.goodsAmount
        })
    },
    addPriceChange(e){
        this.data.agreementPrice = e.detail
        this.setData({
            agreementPrice:this.data.agreementPrice
        })
        this.calculation()
    },
    quantityChange(e){
        this.data.quantity = e.detail
        this.setData({
            quantity:this.data.quantity
        })
        this.calculation()
    },
    phoneChange(e){
        this.data.phone = e.detail.value
        this.setData({
            phone:this.data.phone
        })
    },
    accoManagerChange(e){
        this.data.accoManager = e.detail.value
        this.setData({
            accoManager:this.data.accoManager
        })
    },
    memoChange(e){
        this.data.memo = e.detail.value
        this.setData({
            memo:this.data.memo
        })
    },
    deliveryChange(e){
        this.data.delivery = e.detail
        this.setData({
            delivery:this.data.delivery
        })
    },
    submit(){
            if(this.data.customerId == ""){
                wx.showToast({
                    icon: "none",
                    title: '请选择买家',
                })
                return false
            }
            if(this.data.supplierId == ""){
                wx.showToast({
                    icon: "none",
                    title: '请选择供货商',
                })
                return false
            }
            if(this.data.warehouseId == ""){
                wx.showToast({
                    icon: "none",
                    title: '请选择采购油库',
                })
                return false
            }
            if(this.data.productId == ""){
                wx.showToast({
                    icon: "none",
                    title: '请选择商品',
                })
                return false
            }
            if( this.data.quantity < this.data.minimum  ){
                wx.showToast({
                    icon: "none",
                    title: '购买吨数必须大于等于'  + this.data.minimum,
                })
                return false
            }
            
        if(this.data.submitFlage == true){
            this.data.submitFlage = false
            this.setData({
                submitFlage:this.data.submitFlage
            })
            let settime = setTimeout(()=>{
                    this.data.submitFlage = true
                    this.setData({
                        submitFlage:this.data.submitFlage
                    })
                    clearTimeout(settime)
            },2000)
            let params ={
                "addPrice": this.data.addPrice,
                "customerId": this.data.customerId,
                "delivery": this.data.delivery,
                "linkman": this.data.accoManager,
                "memo": this.data.memo,
                "phone": this.data.phone,
                "price": this.data.price,
                "productId": this.data.productId,
                "quantity": this.data.quantity,
                "status": 0,
                "supplierId": this.data.supplierId,
                "warehouseId": this.data.warehouseId
            }
            save(params).then(res =>{
                wx.showToast({
                    icon: "none",
                    title: '提交成功',
                })
                let settime = setTimeout(()=>{
                    wx.switchTab({
                        url: '/pages/order/order',
                      })
                    clearTimeout(settime)
                },1000)
            })
        }

    }
})